// pages/message/message.js
//获取应用实例
const app = getApp();
var baseurl = app.baseurl.url;
const util = require('../../utils/util.js'); //路径根据自己的文件目录来
var interval = null // 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: '获取验证码', //倒计时 
    currentTime: 60,
    value: '', //输入的是身份证号
    name: '',
    phone: '',
    yzm: '',
    prizeid: ''
  },
  /**
   * 获取输入的是身份证号
   */
  onChange(e) {
    this.setData({
      value: e.detail.value
    })
  },
  onuser(e) {
    // console.log(e.detail.value);
    this.setData({
      name: e.detail.value
    })
  },
  // 手机号
  onphone(e) {
    // console.log(e.detail.value);
    this.setData({
      phone: e.detail.value
    })
  },
  onyzm(e) {
    // console.log(e.detail.value);
    this.setData({
      yzm: e.detail.value
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setStorageSync('yzm', '3366')
    this.setData({
      prizeid: options
    })
  },
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: '重新获取' + currentTime + '秒',
        disabled: true
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)
  },
  // 获取验证码
  getVerificationCode() {
    // 判断手机号是否正确
    if (!util.checkphone(this.data.phone)) {
      // console.log(this.data.phone);
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false
    } else {
      // 
      this.getCode();
      var that = this
      that.setData({
        disabled: true
      })

      // 获取验证码 
      wx.request({
        url: `${baseurl}api/Message/sendMessage`,
        method: 'POST',
        data: {
          phone: this.data.phone,
          openid: wx.getStorageSync("openid"),
        },
        success(res) {

          wx.showToast({
            title: '发送成功',
            icon: 'none'
          })
          wx.setStorageSync('yzm', res.data.data)

        }

      })
    }

  },
  /**
 * 保存
 */
  save() {
    var that = this
    // 姓名
    if (!util.checkname(this.data.name)) {
      wx.showToast({
        title: '请输入真实的姓名',
        icon: 'none'
      })
      return false
    }

    // 身份证
    if (!util.checkIdCard(this.data.value)) {
      wx.showToast({
        title: '请输入正确的身份证号',
        icon: 'none'
      })
      return false
    }
    if (!util.checkphone(this.data.phone)) {
      // console.log(this.data.phone);
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false
    }

    if (this.data.yzm == '' || wx.getStorageSync("yzm") != this.data.yzm) {
      wx.showToast({
        title: '请输入正确的验证码',
        icon: 'none'
      })
      return false
    } else {
      wx.request({
        url: `${baseurl}api/Message/phone`,
        method: 'POST',
        data: {
          phone: this.data.phone,
          openid: wx.getStorageSync("openid"),
          code: this.data.yzm,
          realname: this.data.name,
          idcard: this.data.value

        },
        success(res) {
          console.log(res);
          wx.showToast({
            title: '提交成功',
            icon: 'none'
          })
          wx.request({
            url: `${baseurl}api/Reservation/reserinfo`,
            method: 'POST',
            data: {
              openid: wx.getStorageSync("openid"),
              user_id: wx.getStorageSync("id"),
              prizeid: wx.getStorageSync("prizeid")
            },
            success(res) {
              // console.log(res);
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
              setTimeout(
                function () {
                  wx.reLaunch({
                    url: '/pages/order/order',
                  })
                },1000)

            }

          })

        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})